/*
 * HttpServer.h
 *
 *  Created on: Mar 13, 2012
 *      Author: shaohong
 */

#ifndef HTTPSERVER_H_
#define WEBSERVER_H_

#include <stdint.h>
#include <string>
#include <semaphore.h>

#include "HttpRequest.h"
#include "HttpResponse.h"

using namespace std;


/**
 * The web server that serves the HTTP request and generate HTTP responses
 */
class WebServer {
public:
	WebServer(uint32_t port, string www_root);
	virtual ~WebServer();

	// run the web server
	void run();

    uint32_t getServerPort() const;

    string getWwwRoot() const;

    void service(int connfd);

    void increamentThreadNumber();

    void decrementThreadNumber();

    int getThreadNumber();

	// gracefully shutdown the webserver;
	void gracefulShutDown();

private:
	uint32_t serverPort;
	string www_root;

	int threadNumber;	// the number of running thread
	pthread_mutex_t threadNumberMutex; // the mutex to access running thread number


	bool serverShutingDown;

    void sendResponse(HttpResponse * response, int connfd);


    // serves a http get request
    void doGet(HttpRequest request, int connfd);

    // serves a http put request
	void doPut(HttpRequest request, int connfd, char * dataReadSoFar, int dataLengthReadSoFar);

};

#endif
